package br.com.rglariguet.pioneer.util;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import br.com.rglariguet.pioneer.modelos.Reparo;

public class FiltroBuscaUtil<T> {
	public List<T> listaComFiltro(Reparo reparo) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		List<T> lista1 = new ArrayList<T>();
		
		
		
		for(PropertyDescriptor propertyDescriptor : Introspector.getBeanInfo(Reparo.class).getPropertyDescriptors()){
		    System.out.println(propertyDescriptor.getReadMethod().invoke(reparo));
		}
		
		
//			for(Field field : reparo.getClass().getDeclaredFields()){
//				if (field.isAnnotationPresent(FiltroDePesquisa.class)) {
////					Class type = field.getType();
////					String name = field.getName();
////					System.out.println(type + " "+ name+";");
////					classe.getMethod("get");
////					method.invoke(obj, arg1, arg2,...);
//				}
//			}
//		}
//		
		return lista1;
	}
	
	public static void main(String[] args) throws Exception {
		new FiltroBuscaUtil<Reparo>().listaComFiltro(new Reparo());
	}
	public List<Class<T>> find(String scannedPackage) {
        String scannedPath = scannedPackage.replace(".", "/");
        URL scannedUrl = Thread.currentThread().getContextClassLoader().getResource(scannedPath);
        List<Class<T>> classes = new ArrayList<Class<T>>();
        if (scannedUrl == null) {
            return classes;
        }
        File scannedDir = new File(scannedUrl.getFile());
        for (File file : scannedDir.listFiles()) {
            classes.addAll(find(file, scannedPackage));
        }
        return classes;
    }
	
	
	  private List<Class<T>> find(File file, String scannedPackage) {
	        List<Class<T>> classes = new ArrayList<Class<T>>();
	        String resource = scannedPackage + "." + file.getName();
	        if (file.isDirectory()) {
	            for (File child : file.listFiles()) {
	                classes.addAll(find(child, resource));
	            }
	        } else if (resource.endsWith(".class")) {
	            int endIndex = resource.length() - ".class".length();
	            String className = resource.substring(0, endIndex);
	            try {
	                classes.add((Class<T>)Class.forName(className));
	            } catch (ClassNotFoundException ignore) {
	            }
	        }
	        return classes;
	    }
}
